package cn.stylefeng.guns.modular.hxl.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.hxl.entity.HxlUser;
import cn.stylefeng.guns.modular.hxl.mapper.HxlUserMapper;
import cn.stylefeng.guns.modular.hxl.model.params.HxlUserParam;
import cn.stylefeng.guns.modular.hxl.model.result.HxlUserResult;
import  cn.stylefeng.guns.modular.hxl.service.HxlUserService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.io.Serializable;
import java.util.*;

/**
 * <p>
 * 好想来-员工信息 服务实现类
 * </p>
 *
 * @author 
 * @since 2025-06-30
 */
@Service
public class HxlUserServiceImpl extends ServiceImpl<HxlUserMapper, HxlUser> implements HxlUserService {

    @Resource
    private UserService userService;

    @Override
    public void add(HxlUserParam param){
        HxlUser entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(HxlUserParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(HxlUserParam param){
        HxlUser oldEntity = getOldEntity(param);
        HxlUser newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public HxlUserResult findBySpec(HxlUserParam param){
        return null;
    }

    @Override
    public List<HxlUserResult> findListBySpec(HxlUserParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(HxlUserParam param,String condition){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public ResponseData getExceldata(MultipartFile file) throws Exception {
        String fileName = file.getOriginalFilename();
        User user = userService.getcurrentUser();

        if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
            return ResponseData.error("上传文件格式不正确");
        }

        boolean isExcel2003 = true;
        if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
            isExcel2003 = false;
        }
        InputStream is = file.getInputStream();
        Workbook wb = null;
        if (isExcel2003) {
            wb = new HSSFWorkbook(is);
        } else {
            wb = new XSSFWorkbook(is);
        }
        Sheet sheet = wb.getSheetAt(0);
        HxlUser hxlUserAdd = null;
        List<HxlUser> addList = new ArrayList<>();
        List<HxlUser> userList = this.lambdaQuery().list();
        Map<String,String> userMap = new HashMap<>();
        for(HxlUser obj : userList){
            userMap.put(obj.getUserAcct(),obj.getUserName());
        }
        for (int r = 1; r <= sheet.getLastRowNum()+1; r++) {
            Row row = sheet.getRow(r);
            if (row == null) {
                continue;
            }
            hxlUserAdd = new HxlUser();
            String  userName = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(0));//姓名
            if (ToolUtil.isNotEmpty(userName)) {
                hxlUserAdd.setUserName(userName);
            } else {
                return ResponseData.error("第"+(r+1) +"行第1列不能为空！" );
            }

            String  userAcct = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(1));//分拣员账号
            if (ToolUtil.isNotEmpty(userAcct)) {
                if(ToolUtil.isNotEmpty(userMap.get(userAcct))){
                    continue;
                }
                hxlUserAdd.setUserAcct(userAcct);
            } else {
                return ResponseData.error("第"+(r+1) +"行第2列不能为空！" );
            }

            String  entryDt = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(2));//入职时间
            if (ToolUtil.isNotEmpty(entryDt)) {
                hxlUserAdd.setEntryDt(entryDt);
            } else {
                return ResponseData.error("第"+(r+1) +"行第3列不能为空！" );
            }

            String  postName = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(3));//岗位
            if (ToolUtil.isNotEmpty(postName)) {
                hxlUserAdd.setPostName(postName);
            }
            hxlUserAdd.setCreateUser(user.getUserId());
            hxlUserAdd.setCreateTime(new Date());
            addList.add(hxlUserAdd);
        }

        if(addList.size() > 0){
            this.saveBatch(addList);
        }
        return ResponseData.success();
    }

    private Serializable getKey(HxlUserParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private HxlUser getOldEntity(HxlUserParam param) {
        return this.getById(getKey(param));
    }

    private HxlUser getEntity(HxlUserParam param) {
        HxlUser entity = new HxlUser();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
